<template>
  <d2-container :filename="filename">
    <el-form ref="form" :model="form" label-width="100px">
      <el-form-item label="真实姓名">
        <el-input v-model="form.Realname"></el-input>
      </el-form-item>
      <el-form-item label="昵称">
        <el-input v-model="form.Nickname"></el-input>
      </el-form-item>
      <el-form-item label="用户名">
        {{form.Username}}
      </el-form-item>
      <el-form-item label="最后登录时间">
        {{form.Lastlogintime}}
      </el-form-item>
      <el-form-item label="最后登录IP">
        {{form.Lastloginip}}
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSubmit">保存</el-button>
        <el-button @click="handleCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </d2-container>
</template>

<script>
import { mapActions } from 'vuex'
import util from '@/libs/util'
import {
  GetUserData, SaveUserData
} from '@/api/sys/user'
export default {
  // name 值和本页的 $route.name 一致才可以缓存页面
  name: 'sys-user-edit',
  components: {
    'UserPageHeader': () =>
      import('./componnets/PageHeader'),
    'UserPageMain': () =>
      import('./componnets/PageMain'),
    'UserPageFooter': () =>
      import('./componnets/PageFooter')
  },
  data () {
    return {
      filename: __filename,
      form: {
        Userid: 0,
        Realname: '',
        Nickname: '',
        Username: '',
        Phone: '',
        Lastloginip: '',
        Lastlogintime: ''
      },
      id: this.$route.params.id,
      loading: false
    }
  },
  created: function () {
    this.getFormData()
  },
  methods: {
    ...mapActions('d2admin/page', [
      'closeCurrent'
    ]),
    getFormData () {
      if (this.$route.params.id === '0') {
        return
      }
      this.loading = true
      this.$notify({
        title: '开始请求数据'
      })
      GetUserData({
        id: this.$route.params.id
      })
        .then(res => {
          this.loading = false
          this.$notify({
            title: '数据请求完毕'
          })
          if (res.status !== '200') {
            this.$notify({
              title: res.msg
            })
          } else {
            this.form = res.data
          }
        })
        .catch(err => {
          this.loading = false
          this.$notify({
            title: '数据请求异常'
          })
          console.log('err', err)
        })
    },
    handleSubmit () {
      var _t = this
      util.SaveData(SaveUserData, this.form, _t, function () {
        _t.handleCancel()
      })
    },
    handleCancel () {
      this.closeCurrent({
        vm: this
      })
    }
  }
}
</script>
